<?php

namespace Admin\Controller;
use Mcit\Page;

class PartnerController extends BaseController{

    //首页
    public function index(){
    
        //分页
        $count = M("partner")->order("id")->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $partner = M("partner")->page($nowPage . ',' . $Page->listRows)->order("id")->select();
        
        
        //var_dump($clients);
        //分页结束
    
        $this->assign("partner",$partner);
        $this->display();
    
    }
    
    
    
    //查看客户
    public function detail(){
    
        if(IS_GET){
            $data = M("partner")->where("id='".$_GET['id']."'")->find();
    
            $this->data=$data;
            $this->display();
        }
    
    
    }
    
    //删除客户
    public function del(){
    
        if(IS_GET){
            $result = M("partner")->where("id='".$_GET['id']."'")->delete();
    
    
            if($result){
                $return['state'] = 100;
                $return['msg']   = '删除成功！';
            }else{
                $return['state'] = 200;
                $return['msg']   = '删除失败！';
            }
    
        }else{
            $ids = strsToArray($_POST['ids']);
            $map['id'] = array("IN",$ids);
            $partner = M("partner")->where($map)->select();
            foreach($partner as $val){
    
                $result = M("partner")->where("id='".$val['id']."'")->delete();
            }
    
        }
    
        if($result){
            $return['state'] = 100;
            $return['msg']   = '删除成功！';
        }else{
            $return['state'] = 200;
            $return['msg']   = '删除失败！';
        }
        echo json_encode($return);
        exit;
    
    }
    
    //数据导出
    public function export(){
        //        echo(1323);die;
        $xlsCell  = array(
            array('id','ID'),
            array('companyname','公司名称'),
            array('contactname','联系人'),
            array('telephone','联系电话'),
            array('email','邮箱'),
            array('content','申请内容'),
            array('createtime','申请时间'),
            
            /*             array('email','电子邮件'),
             array('info','详情'), */
    
        );
        $xlsModel = M('partner');
    
        $isall=I('isall'); //是否全部导出
        $idlist=I('idlist'); //需要导出的ID列表
        if ($isall){
            $map=1;
            /*         }else{
             $map['id']=array('in',$idlist); */
        }
    
        $xlsData  = $xlsModel->where($map)->select();
    
    
        $this->exportExcel($xlsCell,$xlsData);
    }
    
    //PHPExcel导出方法
    private  function exportExcel($expCellName,$expTableData){
        $fileName = "合作伙伴申请列表".date('_YmdHis',time());
        $cellNum  = count($expCellName);
        $dataNum  = count($expTableData);
        Vendor('PHPExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel ();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
    
        //填入主标题
        $objPHPExcel->getActiveSheet()->setCellValue('A1', C('COMPANY'));
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('黑体');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A3:AN3')->getFont()->setBold(true);
    
        //填入副标题
        $objPHPExcel->getActiveSheet()->setCellValue('A2', '申请列表(导出日期：'.date('Y-m-d',time()).')');
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体');
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(16);
    
        //设置表头行高
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(35);
        $objPHPExcel->getActiveSheet()->getRowDimension(2)->setRowHeight(22);
        $objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(25);
    
        //设置居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:AN1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2:AN2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    
        //合并单元格
        $objPHPExcel->getActiveSheet()->mergeCells('A1:AN1');
        $objPHPExcel->getActiveSheet()->mergeCells('A2:AN2');
    
        //设置自动换行
        //        $objPHPExcel->getActiveSheet()->getStyle('A3:AK3')->getAlignment()->setWrapText(true);
    
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'3', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+4), $expTableData[$i][$expCellName[$j][0]]);
                $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j].($i+4),$expTableData[$i][$expCellName[$j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
            }
        }
    
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$fileName.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
    
    
    
    
    
    
    
}
